import pygame
import spyral
import math

import Dic


class Cannon(spyral.sprite.Sprite):
    def __init__(self):
        spyral.sprite.Sprite.__init__(self)
        self.barrel = spyral.util.load_image(Dic.images['cannonbarrel2']);
        self.base = spyral.util.load_image(Dic.images['cannonbase'])
        self.basepos = (0,0)
        self.image = spyral.util.new_surface(280, 280);
        self.image.blit(self.barrel, (0,0));
        self.image.blit(self.base, self.basepos);
        self.pos = (-50, 300)
        self.layer = 2;
        self.angle = 0;
        self.x = self.rect.center[0];
        self.y = self.rect.center[1];
        self.originx = self.rect.center[0];
        self.originy = self.rect.center[1];
        self.cannontipx = self.rect.center[0] + 140;
        self.cannontipy = self.rect.center[1];
        self.speed = 0;
        
    def update(self, tick):
        if (-self.image.get_width() < self.pos[0]):
            self.pos = (self.pos[0] - self.speed, self.pos[1]);
        
        
        
    '''
    rotate about center of image--center of image is cannon rotating point
    '''
    def rotate(self, angle):
        self.angle = angle;

        self.cannontipx = self.originx + math.cos(math.radians(self.angle)) * 140;
        self.cannontipy = self.originy - math.sin(math.radians(self.angle)) * 140;
        
        oldcenter = self.rect.center;
        oldsize = self.image.get_size()
        self.image.fill(0)
        self.image = pygame.transform.rotate(self.barrel, self.angle);
        self.rect = self.image.get_rect();
        self.rect.center = oldcenter;
        
        self.basepos = (self.basepos[0] - float((oldsize[0] - self.image.get_size()[0]))/2,
                        self.basepos[1] - float((oldsize[1] - self.image.get_size()[1]))/2)
        self.image.blit(self.base, self.basepos);

    def scrollY(self, yvel):
        self.pos = (self.pos[0], self.pos[1] + yvel)
        
        